/*
 * Copyright (c) 2013 Eugene Kuzmenko
 *
 * Permission  is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in  the Software without restriction, including without limitation the rights
 * to  use,  copy,  modify,  merge, publish, distribute, sublicense, and/or sell
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The  above  copyright  notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * For more information, see <http://www.aljey.com>.
 */

//=../../Exception/BadMethodCallException
//=AbstractButton

/**
 * The button component.
 *
 * @author Eugene Kuzmenko <mail@aljey.com>
 * @class {Button}
 * @extends {AbstractButton}
 */
Class.define('Button', {

  extend: 'AbstractButton',

  /**
   * A button "type".
   *
   * @property {Number}
   * @default 0
   * @example
   *   0 = button
   *   1 = submit
   *   2 = reset
   */
  buttonType: 0,

  /**
   * A button visual style.
   *
   * @property {Number}
   * @default 0
   * @example
   *   0 = default
   *   1 = primary
   *   2 = success
   *   3 = info
   *   4 = warning
   *   5 = danger
   *   6 = link
   */
  style: 0,

  /**
   * A button size.
   *
   * @property {Number}
   * @default 2
   * @example
   *   0 = extra small
   *   1 = small
   *   2 = default
   *   3 = large
   */
  size: 2,

  /**
   * If true spans the entire width of a parent.
   *
   * @property {bool}
   */
  blockLevel: false,

  /**
   * If true the button will look as though it's pressed down.
   *
   * @property {bool}
   */
  pressed: false,

  /**
   * If true will make the button unclickable.
   *
   * @property {bool}
   */
  disabled: false,

  /**
   * Have a button point to a given url.
   *
   * @property {String}
   */
  href: '',

  /**
   * The target of a link.
   *
   * @property {String}
   */
  target: '_blank',

  tpl: [
    '<',
    '<%if(href){%>',
      'a href="<%=href%>" target="<%=target%>" role="button"',
    '<%}else{%>',
      'button type="<%=(["button","submit","reset"])[buttonType]%>"',
        '<%if(disabled){%> disabled="disabled"<%}%>',
    '<%}%>',
      ' class="',
        'btn',
        ' btn-<%=(["default","primary","success","info","warning","danger",',
          '"link"])[style]%>',
        ' <%=(["btn-xs","btn-sm","","btn-lg"])[size]%>',
        '<%if(blockLevel){%> btn-block<%}%>',
        '<%if(pressed){%> active<%}%>',
        '<%if(href&&disabled){%> disabled<%}%>',
      '"',
    '></<%if(href){%>a<%}else{%>button<%}%>>'
  ],

  /**
   * Changes button "type".
   *
   * @param {Number} val A button "type".
   * @returns {Button}
   */
  setButtonType: _static.Container.setter(function (val) {
    this.href || this.el.attr('type', (['button', 'submit', 'reset'])[val]);
  }, 'buttonType', 'number'),

  /**
   * Changes the button style.
   *
   * @param {Number} val A button visual style.
   * @returns {Button}
   */
  setStyle: _static.Container.setter(function (val) {
    var styles = ['default', 'primary', 'success', 'info', 'warning', 'danger',
      'link'];
    this.el.removeClass('btn-' + styles[this.style]).addClass('btn-' +
      styles[val]);
  }, 'style', 'number'),

  /**
   * Changes the button size.
   *
   * @param {Number} val A button size.
   * @returns {Button}
   */
  setSize: _static.Container.setter(function (val) {
    var sizes = ['btn-xs', 'btn-sm', '', 'btn-lg'];
    sizes[this.size] && this.el.removeClass(sizes[this.size]);
    sizes[val] && this.el.addClass(sizes[val]);
  }, 'size', 'number', 2),

  /**
   * Changes block-levelness of a button.
   *
   * @param {bool} val If true spans the entire width of a parent.
   * @returns {Button}
   */
  setBlockLevel: _static.Container.setter(function (val) {
    this.el[val ? 'addClass' : 'removeClass']('btn-block');
  }, 'blockLevel', 'bool'),

  /**
   * Changes the "pressed" state of a button.
   *
   * @param {bool} val If true the button will look as though it's pressed down.
   * @returns {Button}
   */
  setPressed: _static.Container.setter(function (val) {
    this.el[val ? 'addClass' : 'removeClass']('active');
  }, 'pressed', 'bool'),

  /**
   * Changes the disabled state of a button.
   *
   * @param {bool} val If true will make the button unclickable.
   * @returns {Button}
   */
  setDisabled: _static.Container.setter(function (val) {
    this.href ? this.el[val ? 'addClass' : 'removeClass']('disabled') : (val ?
      this.el.attr('disabled', 'disabled') : this.el.removeAttr('disabled'));
  }, 'disabled', 'bool'),

  /**
   * Changes the "href" attribute of a link button.
   *
   * @param {String} val
   * @returns {Button}
   */
  setHref: _static.Container.setter(function (val) {
    if ((!val && this.href) || (val && !this.href)) {
      throw Class.create('BadMethodCallException', {
        message: 'A button can either be a push-button or a link.'
      });
    }
    this.el.attr('href', val);
  }, 'href', 'string', '#'),

  /**
   * Changes the "target" attribute of a link button.
   *
   * @param {String} val
   * @returns {Button}
   */
  setTarget: _static.Container.setter(function (val) {
    this.href && this.el.attr('target', val);
  }, 'target', 'string', '_blank'),

  /**
   * Clicks a button.
   *
   * @param [e] Event
   */
  click: function (e) {
    if (this.href) {
      if ('#' === this.href) {
        if (!this.disabled) {
          _.isFunction(this.handler) && this.handler();
          this.fire('click');
        }
        e && e.preventDefault();
      } else if (this.disabled) {
        e && e.preventDefault();
      }
    } else if (!this.disabled) {
      _.isFunction(this.handler) && this.handler();
      this.fire('click');
    }
  },

  listeners: {
    render: function () {
      var me = this;
      me.el.click(function (e) { me.click(e) });
    },
    show: function () { (1 === this.style) && this.el.focus() },
    hide: function () { this.el.blur() }
  }

});